package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into orders (" +
            "number, status, user_id, address_book_id, " +
            "order_time, pay_method, pay_status," +
            " amount, remark, phone, address, user_name," +
            " consignee,estimated_delivery_time, " +
            "delivery_status, pack_amount, " +
            "tableware_number, tableware_status) VALUE" +
            "(#{number},#{status},#{userId},#{addressBookId}," +
            "#{orderTime},#{payMethod},#{payStatus},#{amount}," +
            "#{remark},#{phone},#{address},#{userName},#{consignee}," +
            "#{estimatedDeliveryTime},#{deliveryStatus},#{packAmount}," +
            "#{tablewareNumber},#{tablewareStatus}) ")
    void insert(Orders orders);


    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);



    @Select("select * from orders where id=#{id}")
    Orders selectOrderById(Long id);

    @Update("update orders set status=6 where id=#{id}")
    void updateById(Long id);


    Page<Orders> selectPage(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 求待派送数量  已接单 -> 3
     * @return
     */
    @Select("select count(*) from orders where status = 3")
    Integer countConfirmed();

    /**
     * 求派送中数量  已接单 -> 4
     * @return
     */
    @Select("select count(*) from orders where status=4")
    Integer countDeliveryInProgress();

    /**
     * 求待接单数量  已接单 -> 2
     * @return
     */
    @Select("select count(*) from orders where status=2")
    Integer countToBeConfirmed();


    /**
     *    根据状态和下单时间查询订单
     * @param status :订单状态
     * @param orderTime : 下单时间
     * @return :返回的是一个订单的集合，因为可能不是一个订单
     */
    @Select("select * from orders where status=#{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrdertimeLt(Integer status, LocalDateTime orderTime);


    /**
     *  根据动态条件统计营业额
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    Integer countByMap(Map map);


    /**
     * 查询商品销量排名
     * @param
     * @param
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
}
